<script setup lang="ts">
import XtxSwiper from '@/components/XtxSwiper.vue'
import CustomNavbar from './componets/CustomNavbar.vue'
import CategoryPanel from './componets/CategoryPanel.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { ref } from 'vue'
import {} from '@/services/home'
import HotPanel from './componets/HotPanel.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from '@/components/PageSkeleton.vue'

//轮播图列表
const bannerList = ref<BannerItem[]>([])
//获取轮播图列表
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

//前台分类列表
const categoryList = ref<CategoryItem[]>([])
//获取前台分类列表
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 热门推荐列表
const hotList = ref<HotItem[]>([])
//获取热门推荐列表
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
// 骨架屏开关
const isLoaading = ref(false)
//uniapp生命周期函数,一进页面就会执行
//页面加载
onLoad(async () => {
  // 数据加载显示骨架屏
  isLoaading.value = true
  // await Promise.all等待多个异步方法同时操作，等异步方法都完成了，才结束
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  // 数据加载完毕，关掉骨架屏
  isLoaading.value = false
})

// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动触底事件 到底后根据当前页码请求数据，每次请求完数据后都会给页码+1，直到页码大于总页数，才给出提示没有更多数据了
const onScrolltolower = () => {
  guessRef.value?.getMore()
}

// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData() // 加载数据
  // await Promise.all等待多个异步方法同时操作，等异步方法都完成了，才结束
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  isTriggered.value = false // 关闭动画
}
</script>
<template>
  <!-- 顶部导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器 @scrolltolower 滚动容器触底事件 -->
  <!-- refresher-enabled开启下拉刷新动画，
  :refresher-triggered控制下拉刷新动画的显示和关闭
   @refresherrefresh下拉刷新触发事件 -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <!-- 骨架屏 其实就是Loaading  -->
    <PageSkeleton v-if="isLoaading" />

    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList" />

      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
// 设置首页底色
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
